# -*- coding:utf-8 -*-
# @Time : 2022-1-11 16:05 
# @Author : suny570
# @Site   : 
# @File : loginfo_engine.py 
# @Software: PyCharm

import src.db_es.es_config as Config

class LoginfoEngine(object):

    def __init__(self, server_handle, model=None):
        self.server_handle = server_handle
        self.index_name = Config.ONLINE_ES_INDEX_SEARCH_LOGS
        self.type_name = Config.ONLINE_ES_TYPE_SEARCH_LOGS
        self.query_data = None
        self.model = model

    def write_search_info_into_es(self, query_data, log_data):
        idx = self.server_handle.count(self.index_name, self.type_name) + 1
        document = {
            "_index": self.index_name,
            "_type": self.type_name,
            "_id": idx,
            "_source": {}
        }
        # 单独的ID 严格区分
        document["_source"]['id'] = idx
        document["_source"]['user_id'] = log_data['ip']
        document["_source"]['content_type'] = query_data['content_type']
        document["_source"]['content'] = query_data["query"]
        document["_source"]['search_result'] = log_data['ret_data']
        document["_source"]['status'] = 1
        document["_source"]['create_time'] = log_data['time']
        document["_source"]['user_reco_degree'] = 1
        document["_source"]['init_search_json'] = log_data['query_info']
        document["_source"]['logs_info_2'] = ''
        document["_source"]['logs_info_3'] = ''
        document["_source"]['logs_info_4'] = ''
        document["_source"]['logs_info_5'] = ''
        document["_source"]['logs_info_6'] = ''
        document["_source"]['logs_info_7'] = ''
        document["_source"]['remarks_1'] = 0
        document["_source"]['remarks_2'] = 0
        document["_source"]['remarks_3'] = 0

        # 新数据入库
        res = self.server_handle.create_index_used_type_id_body(self.index_name, self.type_name, idx, document["_source"])
        if res is not None:
            #print("successed line is \t" + str(idx))
            return True
        else:
            return False
